<?php


namespace Api\Controller;
use Think\Controller;
use Think\Page;
/*
 * 先到先得控制器
 * */
class FreePrizesController extends BaseController
{
	
	public function  __construct() 
	{   
        parent::__construct();
    }
	
	
	/**
	 * 结算活动数据
	 * 
	 */
	public function settlementFreePrizes()
	{ 
	    // 获取活动列表
	    $where['start_time'] = ['ELT',time()];
		$where['end_time'] = ['EGT',time()];
		$where['is_on_sale'] = 1;
		$where['id'] = I('free_prize_id');
		$freePrizeInfo = M('free_prizes')->field('id,start_time,end_time,title,goods_id,integral,ratio,is_on_sale,content,settlement_time,settlement_users_id')->where($where)->find();
		// p($freePrizeInfo);die;
		if($freePrizeInfo){
			if($freePrizeInfo['settlement_users_id'] != 0){
				$this->returnInfo('-1','活动已经结束');
			}
			// 查询活动商品信息
			$goodsInfo = M('goods')->field('goods_id,is_on_sale,goods_name,original_img')->where(['goods_id'=>$freePrizeInfo['goods_id'],'is_on_sale'=>1])->find();
			if($goodsInfo){
				$goodsInfo['original_img'] = parseMediaUrl($goodsInfo['original_img']);
				$freePrizeInfo['goodsInfo'] = $goodsInfo;
				
				// 查询自己是否已经参加了活动
				$freePrizeInfo['is_join_freeprize'] = 0;
				$user_id = $this->user_id;
				if($user_id){
					// 查询参加记录
					$freePrizesUsers = M('free_prizes_users')->where(['free_prizes_id'=>$freePrizeInfo['id'],'users_id'=>$user_id])->find();
					if($freePrizesUsers){
						free_prizes_log($freePrizeInfo['id'], $user_id,'用户发起结算活动');
						update_free_prizes($freePrizeInfo['id'],$user_id,'用户结算主动刷新数据');
						// 获取满足活动条件数据
						$settlementFreePrizesUsers = M('free_prizes_users')->where(['free_prizes_id'=>$freePrizeInfo['id'],'integral'=>['EGT',$freePrizeInfo['integral']]])->find();
						if($settlementFreePrizesUsers){
							// 结算活动
							$res_data = settlement_free_prizes($freePrizeInfo['id'],$settlementFreePrizesUsers['users_id']);
							if($res_data['is_success'] == true){
								$this->returnInfo('1','活动结算成功');
							}else{
								$this->returnInfo('-1','结算失败',$res_data['message']);
							}
							
						}else{
							$this->returnInfo('-1','没有满足条件用户');
						}
					}else{
						$this->returnInfo('-1','请先参加活动');
					}
				}else{
					$this->returnInfo('-100','无登录信息',$freePrizeInfo);
				}
			}else{
				$this->returnInfo('-1','无可用商品');
			}
		}else{
			$this->returnInfo('-1','无可用活动');
		}
	}
	
	/**
	 * 参加最新活动信息
	 * 
	 */
	public function joinFreePrizes()
	{ 
	    // 获取活动列表
	    $where['start_time'] = ['ELT',time()];
		$where['end_time'] = ['EGT',time()];
		$where['is_on_sale'] = 1;
		$where['id'] = I('free_prize_id');
		$freePrizeInfo = M('free_prizes')->field('id,start_time,end_time,title,goods_id,integral,ratio,is_on_sale,content,settlement_time,settlement_users_id')->where($where)->find();
		// p($freePrizeInfo);die;
		if($freePrizeInfo){
			if($freePrizeInfo['settlement_users_id'] != 0){
				$this->returnInfo('-1','活动已经结束');
			}
			// 查询活动商品信息
			$goodsInfo = M('goods')->field('goods_id,is_on_sale,goods_name,original_img')->where(['goods_id'=>$freePrizeInfo['goods_id'],'is_on_sale'=>1])->find();
			if($goodsInfo){
				$goodsInfo['original_img'] = parseMediaUrl($goodsInfo['original_img']);
				$freePrizeInfo['goodsInfo'] = $goodsInfo;
				
				// 查询自己是否已经参加了活动
				$freePrizeInfo['is_join_freeprize'] = 0;
				$user_id = $this->user_id;
				if($user_id){
					// 查询参加记录
					$freePrizesUsers = M('free_prizes_users')->where(['free_prizes_id'=>$freePrizeInfo['id'],'users_id'=>$user_id])->find();
					if($freePrizesUsers){
						$this->returnInfo('-1','已经参加活动');
					}else{
						// free_prizes_log($freePrizeInfo['id'], $user_id,'用户主动参加活动'); // 暂时不使用
						$add['free_prizes_id'] = $freePrizeInfo['id'];
						$add['users_id'] = $user_id;
						$add['integral'] = 0;
						$add['all_pay_money'] = 0;
						$add['add_time'] = time();
						$add['last_update'] = time();
						M('free_prizes_users')->add($add);
						update_free_prizes($freePrizeInfo['id'],$user_id,'用户主动参与活动刷新数据');
						$this->returnInfo('1','活动参与成功');
					}
				}else{
					$this->returnInfo('-100','请先登录',$freePrizeInfo);
				}
			}else{
				$this->returnInfo('-1','无可用商品');
			}
		}else{
			$this->returnInfo('-1','无可用活动');
		}
	}

	/**
	 * 获取最新活动信息
	 * 
	 */
    public function freePrizesInfo()
    { 
		
        // 获取活动列表
        $where['start_time'] = ['ELT',time()];
		// $where['end_time'] = ['EGT',time()];
		$where['is_on_sale'] = 1;
		$freePrizeInfo = M('free_prizes')->field('id,start_time,end_time,title,goods_id,integral,ratio,is_on_sale,content,settlement_time,settlement_users_id')->where($where)->find();
		// p($freePrizeInfo);die;
		if($freePrizeInfo){
			$freePrizeInfo['content'] = json_decode($freePrizeInfo['content']);
			// 查询活动商品信息
			$goodsInfo = M('goods')->field('goods_id,is_on_sale,goods_name,original_img')->where(['goods_id'=>$freePrizeInfo['goods_id'],'is_on_sale'=>1])->find();
			if($goodsInfo){
				$goodsInfo['original_img'] = parseMediaUrl($goodsInfo['original_img']);
				$freePrizeInfo['goodsInfo'] = $goodsInfo;
				
				// 查询自己是否已经参加了活动
				$freePrizeInfo['is_join_freeprize'] = 0;
				$user_id = $this->user_id;
				// p($user_id);die;
				if($user_id){
					update_free_prizes($freePrizeInfo['id'],$user_id,'用户主动刷新数据');
					// 查询参加记录
					$where_free_prize_user['free_prizes_id'] = $freePrizeInfo['id'];
					$where_free_prize_user['users_id'] = $user_id;
					
					$freePrizesUsers = M('free_prizes_users')->where($where_free_prize_user)->find();
					if($freePrizesUsers){
						// p($freePrizesUsers);die;
						$freePrizeInfo['is_join_freeprize'] = 1;
						$freePrizeInfo['freePrizesUsers'] = $freePrizesUsers;
						
					}
				}
				// 获取排行榜前五
				$users_ranking = M('free_prizes_users')->where(['free_prizes_id'=>$freePrizeInfo['id']])->order('integral DESC')->limit(10)->select();
				$freePrizeInfo['users_ranking'] = $users_ranking;
				// 结算后获取订单信息
				if($freePrizeInfo['settlement_users_id'] != 0){
					$freePrizeInfo['settlement_users_nickname'] = M('free_prizes_users')->where(['free_prizes_id'=>$freePrizeInfo['id'],'users_id'=>$freePrizeInfo['settlement_users_id']])->find()['nickname'];
					$freePrizeInfo['settlement_users_order'] = M('free_prizes_users_log')->where(['free_prizes_id'=>$freePrizeInfo['id'],'users_id'=>$freePrizeInfo['settlement_users_id']])->order('add_time DESC')->select();
				}
				// 获取获奖用户信息
				$this->returnInfo('1','获取成功',$freePrizeInfo);

			}else{
				$this->returnInfo('-1','无可用商品');
			}
		}else{
			$this->returnInfo('-1','无可用活动');
		}
		
    }

}